package boofbridgeimaj.examples;

import java.awt.image.BufferedImage;
import java.util.List;

import org.openimaj.image.DisplayUtilities;
import org.openimaj.image.ImageUtilities;
import org.openimaj.image.MBFImage;
import org.openimaj.image.colour.Transforms;
import org.openimaj.image.typography.FontStyle;
import org.openimaj.image.typography.hershey.HersheyFont;
import org.openimaj.video.VideoDisplay;
import org.openimaj.video.VideoDisplayListener;
import org.openimaj.video.capture.Device;
import org.openimaj.video.capture.VideoCapture;
import org.openimaj.video.capture.VideoCaptureException;


import boofbridgeimaj.image.util.BoofToImaj;
import boofbridgeimaj.image.util.ImajToBoof;
import boofcv.alg.filter.blur.GBlurImageOps;
import boofcv.alg.filter.derivative.GImageDerivativeOps;
import boofcv.core.image.GeneralizedImageOps;
import boofcv.core.image.border.BorderType;
import boofcv.gui.image.ShowImages;
import boofcv.gui.image.VisualizeImageData;
import boofcv.io.image.UtilImageIO;
import boofcv.struct.image.ImageFloat32;
import boofcv.struct.image.ImageSingleBand;
import boofcv.struct.image.MultiSpectral;

public class ExampleImajVideoBoofCV {
	public static void main(String[] args) throws VideoCaptureException {
		List<Device> devs = VideoCapture.getVideoDevices();
		Device dev = devs.get(0);
		
		VideoCapture capt = new VideoCapture(320, 240,dev);
		VideoDisplay<MBFImage> disp = VideoDisplay.createOffscreenVideoDisplay(capt);
		disp.addVideoListener(new VideoDisplayListener<MBFImage>() {
			long lastFrame = System.currentTimeMillis();
			ImageFloat32 boofImg = null;
			@Override
			public void beforeUpdate(MBFImage mbfImage) {
				
				boofImg = ImajToBoof.convert(Transforms.calculateIntensityNTSC(mbfImage), boofImg);
				mbfImage = colourizedSobel(boofImg);
				long newFrameTime = System.currentTimeMillis();
				long diff = newFrameTime - lastFrame;
				double fps = 1000.0 / diff;
				mbfImage.drawText(String.format("FPS: %3.2f",fps), 50, 50, HersheyFont.ROMAN_SIMPLEX,24);
				DisplayUtilities.displayName(mbfImage, "colourized by boof");
				lastFrame = newFrameTime;
			}
			
			@Override
			public void afterUpdate(VideoDisplay<MBFImage> display) {
				// TODO Auto-generated method stub
				
			}
		});
	}
	
	public static <T extends ImageSingleBand, D extends ImageSingleBand> MBFImage colourizedSobel( T input )
	{
		Class<T> inputType = (Class<T>)input.getClass();
		Class<D> derivType = GImageDerivativeOps.getDerivativeType(inputType);
 
		T blurred = GeneralizedImageOps.createSingleBand(inputType, input.width, input.height);
		D derivX = GeneralizedImageOps.createSingleBand(derivType, input.width, input.height);
		D derivY = GeneralizedImageOps.createSingleBand(derivType, input.width, input.height);
 
		// Gaussian blur: Convolve a Gaussian kernel
		GBlurImageOps.gaussian(input, blurred, -1, 5, null);
 
		// Calculate image's derivative
		GImageDerivativeOps.sobel(blurred, derivX, derivY, BorderType.EXTENDED);
 
		// display the results
		BufferedImage outputImage = VisualizeImageData.colorizeSign(derivX,null,-1);
		return ImageUtilities.createMBFImage(outputImage, false);
		
	}
}
